package richbar.com.richbar.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.text.TextUtils;
import android.util.Log;

import com.common.util.DateUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;


import richbar.com.richbar.R;

/**
 * Created by Administrator on 2018/11/12.
 */

public class Util {

    /**
     * 判断当前是否有网络
     *
     * @param context
     * @return
     */
    public static boolean checkNet(Context context) {
        boolean flag = false;
        ConnectivityManager connectManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectManager.getActiveNetworkInfo() != null) {
            flag = connectManager.getActiveNetworkInfo().isAvailable();
        }

        return flag;

    }


    /**
     * 登录之前的检测
     *
     * @return
     */
    public static boolean check(Context context) {

        if (!checkNet(context)) {
            ToastUtil.showToast(context, context.getString(R.string.error_net));
            return false;
        }
        return true;
    }

    public static String md5() {
        Log.d("access_token",DateUtil.getCurrentDate(DateUtil.FORMAT_TYPE_DATE)+"/");
        String string = "fuba123!@#321#@!" + DateUtil.getCurrentDate(DateUtil.FORMAT_TYPE_DATE);
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String getDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String doubleMonth = month + "";
        if (month < 10){
            doubleMonth = "0" + month;
        }
        String doubleDay = day + "";
        if (day < 10){
            doubleDay = "0" + day;
        }
        return year + "-" + doubleMonth + "-" + doubleDay;
    }

    /**
     * @param path 图片路径
     * @return
     * @Descriptionmap 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
     * @author temdy
     * @Date 2015-01-26
     */
    public static String imageToBase64(String path) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(path);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
        // 对字节数组Base64编码
        Base64Encoder encoder = new Base64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }
}
